package com.cnhis.cloudhealth.clidoctorweb.gzip;

import java.io.ByteArrayOutputStream;
//package me.gacl.web.filter;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 使用注解标注过滤器
 * @WebFilter将一个实现了javax.servlet.Filte接口的类定义为过滤器
 * 属性filterName声明过滤器的名称,可选
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 */
//@sdfsdWebFilter(filterName="zipFilter1",urlPatterns="/ddddddddddddddd*")
@Deprecated
public class zipFilter2 implements Filter {

    @Override
    public void destroy() {
        System.out.println("过滤器销毁");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response1,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("执行过滤操作");
      //  chain.doFilter(request, response1);
        
        //GZIP压缩：  //压缩输出：把源数据通过GZIPOutputStream压缩到ByteArrayOutputStream中  
        /*
         *     ByteArrayOutputStream baout=new ByteArrayOutputStream();  
        GZIPOutputStream gzip=new GZIPOutputStream(baout);  
        gzip.write(str.getBytes("utf-8"));  
        gzip.close();  
         * */
        
    
        MyResponseWrapper responseWrapper = new MyResponseWrapper((HttpServletResponse) response1);
       
        chain.doFilter(request, responseWrapper);
      byte[] bytearr1=  responseWrapper.getMyOutputStream().ByteArrayOutputStream1.toByteArray();
      System.out.println( " ------------- bytearr1 length:"+bytearr1.length);
      //  if (myWriter != null) {
     //       String content = myWriter.getContent();
            
        
        
        HttpServletResponse response=(HttpServletResponse) response1;
        
    //    response.get
      //  response.getby
   //      byte[] buff = response.getBufferedBytes() ;  
        //创建缓存容器：
       
        GZIPOutputStream gzip = new GZIPOutputStream( new ByteArrayOutputStream()) ;
        gzip.write(bytearr1);  
        gzip.close() ;
        
        
         
        //设置响应头；
        response.setHeader("Content-Encoding", "gzip");
        response.setContentLength(bytearr1.length) ;  
       response.getOutputStream().write( bytearr1) ;
        
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("过滤器初始化");
    }
}